package com.example.city_home_app.Fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.city_home_app.Activity.MyFocusActivity;
import com.example.city_home_app.Bean.Down_MyFocus;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_UserFans;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_UserFocus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class Fragment_FansUser extends BaseFragment {
    @BindView(R.id.rec_userinfo_post)
    RecyclerView recUserinfoPost;
    @BindView(R.id.refresh_userinfo_post)
    SwipeRefreshLayout refreshUserinfoPost;
    Unbinder unbinder;
    private Down_MyFocus down_myFocus;
    private MyFocusActivity instance;
    private Adapter_UserFans adapter_userFans;
    @Override
    protected void init(View view) {
        unbinder = ButterKnife.bind(this, view);
        instance = (MyFocusActivity) getActivity();

        recUserinfoPost = (RecyclerView) view.findViewById(R.id.rec_userinfo_post);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recUserinfoPost.setLayoutManager(layoutManager);
        adapter_userFans = new Adapter_UserFans(getActivity());
        recUserinfoPost.setAdapter(adapter_userFans);
        recUserinfoPost.setItemAnimator(new DefaultItemAnimator());

        refreshUserinfoPost = (SwipeRefreshLayout) view.findViewById(R.id.refresh_userinfo_post);
        refreshUserinfoPost.setColorSchemeColors(0xff49afcd);
        refreshUserinfoPost.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        instance.updatedata();
                        down_myFocus = instance.getDown_myFocus();
                        adapter_userFans.setList(down_myFocus.getFans());
                        adapter_userFans.notifyDataSetChanged();
                        refreshUserinfoPost.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        down_myFocus = instance.getDown_myFocus();
        adapter_userFans.setList(down_myFocus.getFans());
        adapter_userFans.notifyDataSetChanged();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_userinfo_post;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
